package controler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.reports.ReportBean;
import model.reports.ReportConfigBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import business.Estadisticas;
import business.misc.Util;

public class Statistics extends MappingDispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
    	String str_forward = Util.DEFAULT;
    	ArrayList arrL_retVal = new ArrayList();
    	Map map_reportes = ReportConfigBean.getInstance().getReports();
    	Iterator itr_tmp = map_reportes.keySet().iterator();
    	while( itr_tmp.hasNext() ){
    		arrL_retVal.add(map_reportes.get(itr_tmp.next()));
    	}
    	request.setAttribute("map_reportes", arrL_retVal);
		return mapping.findForward(str_forward);
	}
	
	public ActionForward browse(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		String str_forward = Util.DEFAULT;
		String str_code = request.getParameter("code");
		HttpSession session = request.getSession();
		if( str_code == null  ){
			str_code =( (ReportBean)session.getAttribute("bn_reporte")).getCode();
		}
		ReportBean report = Estadisticas.existeReporte( str_code );
		if( report != null ){
			request.setAttribute("arrL_report", model.Statistics.getReportData(report) );
			session.setAttribute("bn_reporte", report);
		}
		return mapping.findForward(str_forward);
	}
}
